package com.example.demo.thread.create;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


/**
 * @author MarioZzz
 */
public class Pool {


    /**
     * 7 使用线程池创建线程
     * @param args
     */
    public static void main(String[] args) {

        //创建线程池
        ExecutorService pool = Executors.newCachedThreadPool();

        //线程池中分配第一个任务，具体由哪个线程执行交给线程池控制
        //任务执行的先后顺序也不受控制
        pool.submit(new Runnable() {
            @Override
            public void run() {
                //执行业务逻辑
                for(int i = 1; i <= 100; i++) {
                    System.out.println("线程:" + Thread.currentThread().getName() + "执行了任务A" + i + "~");
                }
            }
        });

        //第二个任务
        pool.submit(new Runnable() {
            @Override
            public void run() {
                //执行业务逻辑
                for(int i = 101; i <= 200; i++) {
                    System.out.println("线程:" + Thread.currentThread().getName() + "执行了任务B" + i + "~");
                }
            }
        });

        //第三个任务
        pool.submit(new Runnable() {
            @Override
            public void run() {
                //执行业务逻辑
                for(int i = 201; i <= 300; i++) {
                    System.out.println("线程:" + Thread.currentThread().getName() + "执行了任务C" + i + "~");
                }
            }
        });
    }
}
